package com.thesis.client;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.thesis.object.Bill;
import com.thesis.object.BillDescription;
import com.thesis.object.Dish;
import com.thesis.object.DishDetailAddBill;
import com.thesis.object.Session;
import com.thesis.utils.Defines;
import com.thesis.utils.Utils;

public class FoodsAdapter extends BaseAdapter {
	private final Context context;
	private final ArrayList<Dish> listDishes;
	
	private ArrayList<DishDetailAddBill> arr_food_addbill;
	private ArrayList<DishDetailAddBill> arr_food_modifybill;
	private ArrayList<DishDetailAddBill> arr_food_deletebill;
	private Bill bill;
	private boolean isHave;
	
	

	public FoodsAdapter(Context context, ArrayList<Dish> listDishes,ArrayList<DishDetailAddBill> arr_food_addbill,
			ArrayList<DishDetailAddBill> arr_food_modifybill, ArrayList<DishDetailAddBill> arr_food_deletebill, Bill bill) {
	
		this.context = context;
		this.listDishes = listDishes;
		this.arr_food_addbill = arr_food_addbill;
		this.arr_food_modifybill = arr_food_modifybill;
		this.arr_food_deletebill = arr_food_deletebill;
		this.bill = bill;

	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View rowView;
		if (convertView == null) {
			rowView = new View(context);
			rowView = inflater.inflate(R.layout.item_food, parent, false);
		} else {
			rowView = (View) convertView;
		}
		isHave = false;
		
		ImageView iv_food = (ImageView) rowView.findViewById(R.id.if_iv_iconfood);
		TextView tv_foodname = (TextView) rowView.findViewById(R.id.if_tv_foodname);
		TextView tv_foodcost = (TextView) rowView.findViewById(R.id.if_tv_foodcost);
		final TextView tv_foodnumber = (TextView) rowView.findViewById(R.id.if_tv_foodnumber);
		final EditText et_foodnote = (EditText) rowView.findViewById(R.id.if_et_noteofdish);
		ImageView iv_bonus = (ImageView) rowView.findViewById(R.id.if_iv_bonus);
		ImageView iv_minus = (ImageView) rowView.findViewById(R.id.if_iv_minus);
		final ImageView btn_check = (ImageView) rowView.findViewById(R.id.if_btn_checked);
		final ImageView btn_cancel = (ImageView) rowView.findViewById(R.id.if_btn_cancelhecked);
		ImageView btn_detail = (ImageView) rowView.findViewById(R.id.if_btn_detail);
		String imagePath;
		tv_foodname.setText(listDishes.get(position).getName());
		tv_foodcost.setText(listDishes.get(position).getCost() + " " +
				listDishes.get(position).getUnit());
		if (!listDishes.get(position).getImage().equals("")){
			imagePath = Environment.getExternalStorageDirectory().toString() 
					+ "/restautant/dish/" +listDishes.get(position).getImage();
			iv_food.setImageDrawable(Drawable.createFromPath(imagePath));
		} else {
			iv_food.setImageResource(R.drawable.no_image);
		}
		if (Session.getInstance().getRole() != 0) {
			tv_foodnumber.setVisibility(View.GONE);
			iv_bonus.setVisibility(View.GONE);
			iv_minus.setVisibility(View.GONE);
			btn_check.setVisibility(View.GONE);
			btn_cancel.setVisibility(View.GONE);
			et_foodnote.setVisibility(View.GONE);
			
			
			
		} else{
			
			
			ArrayList<BillDescription> arr_current = bill.getListDishDes().getListBill();
			
			if (arr_current.size() == 0 ){
				// check co mon' nao trong bill hay chua 
				
			}		
			else {
				
				for (int i = 0 ; i < arr_current.size();i++){
					if(listDishes.get(position).getId() == arr_current.get(i).getFoodId()){
						//check trong thuc don da goi co mon' nay` chua 
						btn_check.setVisibility(View.INVISIBLE);
						et_foodnote.setText(arr_current.get(i).getFoodNote());
					
						tv_foodnumber.setText(""+arr_current.get(i).getNumber());
						if (arr_current.get(i).getServeTime().equals(null)){
							// neu co thi check thu mon do co dc phuc vu hay chua
						
							iv_minus.setVisibility(View.INVISIBLE);						
							btn_cancel.setVisibility(View.INVISIBLE);
							
						}else{
							
							iv_minus.setVisibility(View.VISIBLE);						
							btn_cancel.setVisibility(View.VISIBLE);
						}
						isHave = true;
						
						break;
					}
					else {
						
					}	
					
					
				}
			}
			
			if(isHave == false ){
				
				// neu mon nay chua co trong bill ;
				btn_check.setVisibility(View.VISIBLE);
				btn_cancel.setVisibility(View.INVISIBLE);
				tv_foodnumber.setText("1");
			}
			
			
		
		}
		// get lishdish current of a billid
		
		// set number and button for each dish in menu for each bill
		
		
//---------------------------------------------------------------------------------------------
		iv_bonus.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {		
				int cur = Integer.parseInt(tv_foodnumber.getText().toString());
				cur ++;
				tv_foodnumber.setText(Integer.toString(cur));
				btn_check.setVisibility(View.VISIBLE);
				btn_cancel.setVisibility(View.INVISIBLE);
			}
		});
		
		iv_minus.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				int cur = Integer.parseInt(tv_foodnumber.getText().toString());
				
				if (cur > 1) {
					cur --;
					tv_foodnumber.setText(Integer.toString(cur));
					btn_check.setVisibility(View.VISIBLE);
					btn_cancel.setVisibility(View.INVISIBLE);
				}
				else {
					
				}
				
				
			}
		});
		
		btn_detail.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(context,FoodDetailActivity.class);
				intent.putExtra(Defines.ID_OF_FOODDETAIL, listDishes.get(position).getId());
				 v.getContext().startActivity(intent);

			}
		});
		
		btn_cancel.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
			
				if(!Utils.sharedInstance().isHaveinBill(listDishes.get(position), bill)){
					// neu mon co trong list add ma chua co trong bill thi xoa ra khoi list add
					for (int i = 0 ; i < arr_food_addbill.size();i++){
						if (arr_food_addbill.get(i).getId() == listDishes.get(position).getId()){
							arr_food_addbill.remove(i);
							break;
						}
					}					
				} else {	
					// neu mon do da con trong bill thi them vao list remove
					arr_food_deletebill.add(new DishDetailAddBill(listDishes.get(position).getId(),0,""));											
				}
				btn_cancel.setVisibility(View.INVISIBLE);
				btn_check.setVisibility(View.VISIBLE);
				
			}
		});

		
		btn_check.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
						
				int cur = Integer.parseInt(tv_foodnumber.getText().toString());
				if (!Utils.sharedInstance().isHaveinBill(listDishes.get(position), bill)){
					// neu mon do chua co trong bill thi them vao list add
					
					arr_food_addbill.add(new DishDetailAddBill(listDishes.get(position).getId(),
							cur, et_foodnote.getText().toString()));
				}
				else {
					//neu mon do co trong bill thi them vao list modify
					arr_food_modifybill.add(new DishDetailAddBill(listDishes.get(position).getId(),cur,et_foodnote.getText().toString()));
				}
												
				btn_check.setVisibility(View.INVISIBLE);
				btn_cancel.setVisibility(View.VISIBLE);
			
			}
		});
		
		return rowView;
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return listDishes.size();
	}

	

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}
}
